package com.lz.controller;


import com.lz.annotation.ExecuteTime;
import com.lz.domain.Tblconsumer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@Api(value = "Hello 测试 和 redis测试")
public class Demo1 {
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value="Hello lz 测试", notes="无注意事项")
    @RequestMapping(value = "/testHello")
    @ExecuteTime(name = "hello测试接口")
    public String testHello() throws InterruptedException {
        Thread.sleep(2000);
        return "Hello lz";
    }

    @ApiOperation(value="Hello lz 2 测试", notes="无注意事项")
    @RequestMapping(value = "/testHello2",method = RequestMethod.POST)
    public String testHello2() {
        return "Hello lz 2";
    }

    @ApiOperation(value="Hello lz 3 测试", notes="无注意事项")
    @RequestMapping(value = "/testHello3",method = RequestMethod.POST)
    public String testHello3() {
        return "Hello lz 3";
    }

    @ApiOperation(value="Hello lz 4 测试", notes="无注意事项")
    @RequestMapping(value = "/testHello4",method = RequestMethod.POST)
    public String testHello4() {
        return "Hello lz 4";
    }

    @ApiOperation(value="redis 测试", notes="无注意事项")
    @RequestMapping(value = "/redisTest",method = RequestMethod.POST)
    public String testRedis() {
        //设置值到redis

        Tblconsumer tblconsumer = new Tblconsumer();


            redisTemplate.opsForList().rightPush("nameList",new Random().toString());
        while (100 < redisTemplate.opsForList().size("nameList")){
            redisTemplate.opsForList().leftPop("nameList");
        }
//        redisTemplate.opsForList().
//            if (100<redisTemplate.opsForList().size("nameList")){
//    //            redisTemplate.opsForList().leftPop("nameList");
//                redisTemplate.opsForList().trim("nameList",0,49);
//                System.out.println("当前线程名称："+ Thread.currentThread().getName()+"==>当前if条件内缓存数量："+redisTemplate.opsForList().size("nameList"));
//            }

//        System.out.println("当前线程名称："+ Thread.currentThread().getName()+"==>当前缓存数量："+redisTemplate.opsForList().size("nameList"));
        return "success";
//        redisTemplate.opsForValue().set("name", "lucy");
//        //从redis获取值
//        return (String) redisTemplate.opsForValue().get("name");
    }

    @ApiOperation(value="redis 测试", notes="redis中要有key k1 且有value值")
    @RequestMapping(value = "/redisGet",method = RequestMethod.POST)
    public String testRedisGet() throws Exception {
        ExecutorService executorService = Executors.newCachedThreadPool();
        return "retry";
    }
}
